package com.cn.lotary.base.web;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author user192
 */
public abstract class BaseLoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    /**
     * example:faces/login.xhtml
     *
     * @return login page full path
     */
    protected String loginPage() {
        return "faces/login.xhtml";
    }

    /**
     * example: key:logined ,value:true
     *
     * @return login session token
     */
    protected abstract String loginToken();

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //TODO:try
        HttpServletRequest hsr = (HttpServletRequest) request;
        Object session = hsr.getSession(true).getAttribute(loginToken());

        Boolean logined = session == null ? false : Boolean.valueOf(session.toString());

        String requestURI = hsr.getRequestURI();
        String webRoot = requestURI.substring(1);
        String webRootPath = "/" + webRoot.substring(0, webRoot.indexOf("/") + 1);

        if (requestURI.contains("login")) {
            chain.doFilter(request, response);
        } else {
            if (logined) {
                String servletPath = hsr.getServletPath();
                if (!servletPath.startsWith("/" + facesServlet())) {
                    servletPath = requestURI.replace(webRootPath, "/" + facesServlet() + "/");
                    hsr.getServletContext().getRequestDispatcher(servletPath).forward(request, response);
                } else {
                    chain.doFilter(request, response);
                }

            } else {
                go(response, webRootPath);
                //following code can not working under the html iframe
                //hsp.sendRedirect("faces/login.xhtml");
                //hsr.getServletContext().getRequestDispatcher("/faces/login.xhtml").forward(request, response);
            }
        }
    }

    /**
     * <code><servlet-mapping></code>
     * <code><servlet-name>Faces Servlet</servlet-name></code>
     * <code><url-pattern>/faces/*</url-pattern></code>
     * <code></servlet-mapping></code>
     *
     * default: faces
     *
     * @return mapping
     */
    protected static String facesServlet() {
        return "faces";
    }

    @Override
    public void destroy() {
    }

    private void go(ServletResponse response, String webRootPath) throws IOException {
        java.io.PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<script>");
        out.println("window.open ('" + webRootPath + loginPage() + "','_top')");
        out.println("</script>");
        out.println("</html>");
    }
}
